import { loginWexin } from '../utils/wexin';
import { handler } from '../business/error';

const appId = process.env.appId;

export default {
    namespace: 'auth',
    state: {
        openId : '',
        redirect: '', //鉴权跳转地址
        phone: '',
        valiCode: '',
        disableGetCode: false, //等待获取验证码网络状态
        canGetCode: false,
        timeRemain: 60,
        showBindMode: false,
        loaddingFlat: false
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        // TODO 暂时没用到这里
        // 登录小程序
        *loginMiniApp({}, { put, select }) {
            const data = yield handler.silentAuth();
            
            // 如果已经绑卡 回到初始页面
            if (data.profile.phone) {
                yield put({
                    type: 'login',
                    payload: data
                });

                //打点
                const params = yield select(state => state.router);
                yield put({
                    type: 'analyse/set',
                    payload: {
                        name: 'login_finish',
                        data: {
                            class: 2,
                            place: params
                        }
                    }
                });
            } else {
                console.log(48);
                wx.showToast({
                    title: '请先绑定手机号',
                    icon: 'none',
                    duration: 2000
                });
            }
        },
        //登录C端返回用户数据，同时如果是新用户就初始化
        *login({payload}, { select, put }) {
            const data = yield put.resolve({
                type: '$request/post',
                payload: {
                    url: '/web/api/healthapp-mobile/v1/user/login'
                }
            });
            const openId = yield select(state => state.auth.openId);
            yield put({
                type: 'save',
                payload: {
                    userInfo: data
                }
            });
            if (data.phone) {
                data.userId = data.userIdStr;
                data.openId = openId;
                yield put.resolve({
                    type : '$storage/set',
                    payload : { key: 'userInfo', value: data }
                });
                yield put({
                    type: 'done'
                });
            }
            if (data.isInit == '0') {
                console.log('此用户为新用户');
            }
        },

        *done({payload}, { put }) {
            const stack = getCurrentPages();
            const last = stack[stack.length - 2];
            last.$component && last.$component.componentWillMount();

            yield put({
                type: '$router/back'
            });
            // wx.nextTick()
        },

        // 微信绑定手机号快速登录
        *bindPhoneSmart({ payload }, { put, select }) {
            try {
                const code = yield loginWexin();
                payload.jsCode = code;
                yield put.resolve({
                    type: '$request/post',
                    payload: {
                        url: '/public/wechat/decrypt_data_and_bind_phone',
                        data: payload
                    }
                });
                yield put({ type: 'auth/login' });

                //打点
                const params = yield select(state => state.router);
                yield put({
                    type: 'analyse/set',
                    payload: {
                        name: 'login_finish',
                        data: {
                            class: 2,
                            place: params
                        }
                    }
                });
            } catch (e) {}
        },
        // 获取验证码
        *getValidateCode({ payload }, { put, call }) {
            yield put({
                type: '$request/get',
                payload: {
                    url: '/sso/passport/smscode',
                    data: payload
                }
            });
            yield put({
                type: 'save',
                payload: {
                    canGetCode: true,
                    timeRemain: 60
                }
            });
        },
        // 手动输入手机号绑定账号
        *bindPhone({ payload }, { put, select }) {
            yield put.resolve({
                type: '$request/get',
                payload: {
                    url: '/sso/passport/bind_phone_smart',
                    data: payload
                }
            });
            yield put({
                type: 'save',
                payload: {
                    showBindMode: false
                }
            });
            const params = yield select(state => state.router);
            yield put({
                type: 'analyse/set',
                payload: {
                    name: 'login_finish',
                    data: {
                        class: 1,
                        place: params
                    }
                }
            });
            yield put({
                type: 'login'
            });
        }
    }
};
